<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

if( $do == 'index' )
{
	$tree = get_category( 'article' );
	$empty = array(
		'id' => 0,
		'name' => '',
		'model' => '',
		'dir' => '',
		'images' => '',
		'keywords' => '',
		'description' => '',
		'index_temp' => '',
		'list_temp' => '',
		'detail_temp' => '',
		'list_rule' => '',
		'detail_rule' => '',
		'workflow_id' => '',
		'list_no' => '',
		'comment_money' => '',
		'ifshow' => '',
		'onepage' => '',
		'document_no' => 0,
		'child' => 0,
		'order' => '',
		'post' => '',
		'post_group_ids' => '',
		'attribute_extend' => ''
	);
	$datas = count( $tree ) > 1?get_category_detial( $tree[1]['id'] ):$empty;
	include( display( 'category_list' ) );
}
if( $do == 'detail' )
{
	$catid = get_post( 'catid' );
	$datas = get_category_detial( $catid );
	echo json_encode( $datas );
}
elseif( $do == 'getcategory' )
{
	$model = get_post( 'model' );
	$tree = get_category( $model );
	echo json_encode( $tree );
}
elseif( $do == 'update' )
{
	$catid = get_post( 'catid' );
	$model = get_post( 'model' );
	$data = get_post( 'data' );
	$data = json_decode( stripslashes( $data ), true );

	$sql = "SELECT `pid`, `dir` FROM `".DB_PRE."category` WHERE `id` = $catid";
	$result = $db->query( $sql, true );

	if( $result['dir'] != $data['dir'] )
	{
		if( $result['pid'] != 0 )
		{
			$p_sql = "SELECT	 `path` FROM `".DB_PRE."category` WHERE `id` = ".$result['pid'];
			$p_result = $db->query( $p_sql, true );
			$old_path = $path= $p_result['path'];

			$path = implode( '/', $path );
			array_pop( $path );
			$new_path = '';
			foreach( $path as $v )
			{
				$new_path .= $v.',';
			}
			$new_path .= $data['dir'];	
		}
		else
		{
			$old_path = $result['dir'];
			$new_path = $data['dir'];
		}
		$data['path'] = $new_path;
		rename_dir( SITE_PATH.$old_path, SITE_PATH.$data['path'] );
	}

	$update_result = $db->update( DB_PRE.'category', $data, array( 'id' => $catid ) );
	
	cache_category( $model );

	( $model != $data['model'] ) && cache_category( $data['model'] );

	echo $update_result?1:0;
}
elseif( $do == 'new' )
{
	$pid = get_post( 'pid' );
	$model = get_post( 'model' );
	$empty = array(
			'id' => 0,
			'attribute_extend' => 0,
			'model' => $model
	);
	$datas = $pid==0?$empty:get_category_detial( $pid );
	include( display( 'category_new' ) );
}
elseif( $do == 'add' )
{
	$type = get_post( 'type' );
	$data = get_post( 'data' );
	$data = json_decode( stripslashes( $data ), true );
	if( $type == 1 )
	{
		$result = insert_category( $data );
		$flag = $result?1:0;
	}
	else
	{
		$categorys = $data['name'];
		$categorys_arr = array();
		$ids_arr = array();
		$insert_id = 0;
		$flag = 1;
		foreach( $categorys as $k => $v )
		{
			$datas = $data;
			$datas['name'] = trim( $v['name'] );
			$datas['dir'] = trim( $v['dir'] );
			$c = true;
			if( my_substr( $v['name'], 2 ) == '  ' || my_substr( $v['name'], 3 ) == '   ' )
			{
				$c = false;
				$datas['pid'] = $insert_id;
			}
			$result = insert_category( $datas );
			if( $result )
			{
				$c && $insert_id = $result;
				$ids_arr[] = $result;
				$c = true;
			}
			else
			{
				if( $ids_arr != '' ) delete_category( $ids_arr );
				$flag = 0;
				break;
			}
			unset( $datas );
		}
	}
	$flag == 1 && cache_category( $data['model'] );
	echo $flag;
}
elseif( $do == 'deleteset' )
{
	$catid = get_post( 'catid' );
	$model = get_post( 'model' );
	include( display( 'category_delete' ) );
}
elseif( $do == 'delete' )
{
	$catid = get_post( 'catid' );
	$model = get_post( 'model' );
	$doc_handle = get_post( 'dochandle' );
	$doc_move_id = get_post( 'docmoveid' );
	$child_handle = get_post( 'childhandle' );
	$child_move_id = get_post( 'childmoveid' );
	$child_category = get_category_childs( $catid, $model, 1 );
	$i = true;
	if( $child_category != '' )
	{
		if( $child_handle == 'true' )
		{
			// todo: 移动子栏目
			foreach( $child_category as $v )
			{
				!move_category( $v['id'], $v['pid'], $v['path'], $child_move_id ) && $i = false;
			}
		}
		else
		{
			// todo: 删除子栏目
			$child_category = get_category_childs( $catid, $model, 0 );
			foreach( $child_category as $v )
			{
				if( $doc_handle == 'true' )
				{
					!move_category_document( $v['id'], $doc_move_id ) && $i = false;
				}
				else
				{
					!empty_category_document( $v['id'] ) && $i = false;
				}
				!delete_category( $v['id'], 0, $v['path'] ) && $i = false;
			}
		}
	}
	$doc_result = $del_result = true;
	if( $catid != 0 )
	{
		$doc_result = $doc_handle == 'true'?move_category_document( $catid, $doc_move_id ):empty_category_document( $catid );
		$sql = "SELECT `pid`, `path` FROM `".DB_PRE."category` WHERE `id` = $catid AND `model` = '$model'";
		$rs = $db->query( $sql, true );
		$del_result = delete_category( $catid, $rs['pid'], $rs['path'] );
	}
	cache_category( $model );
	echo ( $i && $doc_result && $del_result )?1:0;
}
elseif( $do == 'movecategoryset' )
{
	$frame = get_post( 'frame' );
	$catid = get_post( 'catid' );
	$datas = get_category_detial( $catid );
	$model = get_post( 'model' );
	$pid = get_post( 'pid' );
	$tree = get_category( $model );
	include display( 'category_move' );
}
elseif( $do == 'movecategory' )
{
	$catid = get_post( 'catid' );
	echo 1;
}